package com.aos.erp.schools.rpc.sys;

import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.schools.enums.student.sys.DepartmentType;
import com.aos.erp.schools.service.sys.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @program: erp
 * @description: 部门组织controller
 * @author: tanzhiyu
 * @create: 2018-11-01 19:27
 **/
@Api(value = "部门组织service", tags = {"部门组织service"})
@RestController("RPCDepartmentController")
@RequestMapping("/service/department")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;

    @ApiOperation(value = "新增部门组织---车管所")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "divisionCode", value = "分所组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = " 分所名称", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/addDepDivision")
    public ResultEntity addDepDivision(String divisionCode, String name,String appCode) throws Exception {
        if (StringUtils.isBlank(divisionCode) || StringUtils.isBlank(name)) {
            return ResultEntity.failed("分所code或分所名称不能为空", ResultEntity.CODE_NULL);
        }
        departmentService.addBeanDivision(divisionCode, name,appCode);
        return ResultEntity.success();
    }

    @ApiOperation(value = "新增部门组织---总校")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "divisionCode", value = "分所组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "schoolCode", value = "驾校组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = " 驾校名称", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/addDepSchool")
    public ResultEntity addDepSchool(String divisionCode, String schoolCode, String name,String appCode) throws Exception {
        if (StringUtils.isBlank(divisionCode) || StringUtils.isBlank(schoolCode) || StringUtils.isBlank(name)) {
            return ResultEntity.failed("分所code或驾校名称不能为空", ResultEntity.CODE_NULL);
        }
        departmentService.addBeanSchool(divisionCode, schoolCode, name, appCode);
        return ResultEntity.success();
    }

    @ApiOperation(value = "新增部门组织---分校/报名点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentSchoolCode", value = "父级驾校组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "schoolCode", value = "驾校组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "驾校/报名点名称", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "depType", value = " 部门类型[F:分校;B:报名点]", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/addDepBranch")
    public ResultEntity addDepBranch(String parentSchoolCode,String schoolCode, String name,String depType,String appCode) throws Exception {
        if (StringUtils.isBlank(parentSchoolCode) || StringUtils.isBlank(schoolCode) || StringUtils.isBlank(name)) {
            return ResultEntity.failed("总校code或名称不能为空", ResultEntity.CODE_NULL);
        }
        departmentService.addBeanSchoolBranch(parentSchoolCode,schoolCode, name,depType,appCode);
        return ResultEntity.success();
    }

    /*@ApiOperation(value = "新增部门组织---部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depCode", value = "部门组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/addDepDepartment")
    public ResultEntity addDepDepartment(String depCode, String name) throws Exception {
        if (StringUtils.isBlank(depCode) || StringUtils.isBlank(name)) {
            return ResultEntity.failed("部门组织code或名称不能为空", ResultEntity.CODE_NULL);
        }
        departmentService.addBeanSchoolBranch(depCode, name, DepartmentType.department.getValue().toString());
        return ResultEntity.success();
    }*/

    @ApiOperation(value = "删除部门组织")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depId", value = "部门ID", required = true, dataType = "String", paramType = "Query"),
    })
    @PostMapping("/delDep")
    public ResultEntity delDep(String depId,String appCode) throws Exception {
        if (StringUtils.isBlank(depId)) {
            return ResultEntity.failed("部门ID不能为空", ResultEntity.CODE_NULL);
        }
        int i = departmentService.isDel(depId, appCode);
        if (i > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.success("删除失败");
        }
    }

    @ApiOperation(value = "获取分所组织code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "分所代码", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getDepCodeDivision")
    public ResultEntity getDepCodeDivision(String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            return ResultEntity.failed("分所代码不能为空", ResultEntity.CODE_NULL);
        }
        String type = DepartmentType.division.getValue().toString();
        return ResultEntity.success(type + code);
    }

    @ApiOperation(value = "获取总校组织code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "divisionCode", value = "分所组织code", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getDepCodeSchool")
    public ResultEntity getDepCodeSchool(String divisionCode) throws Exception {
        String depCodeSchool = departmentService.getDepCodeSchool(divisionCode);
        return ResultEntity.success(depCodeSchool);
    }

    @ApiOperation(value = "获取分校/报名点组织code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolCode", value = "总校组织code、分校组织code", required = true, dataType = "String", paramType = "Query"),
            @ApiImplicitParam(name = "depType", value = " 部门类型[F:分校;B:报名点]", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getDepCodeBranch")
    public ResultEntity getDepCodeBranch(String schoolCode, String depType) throws Exception {
        if (StringUtils.isBlank(schoolCode) ) {
            return ResultEntity.failed("驾校组织code不能为空", ResultEntity.CODE_NULL);
        }
        if( StringUtils.isBlank(depType)){
            return ResultEntity.failed("部门类型[F:分校;B:报名点]不能为空", ResultEntity.CODE_NULL);
        }
        String depCodeSchool = departmentService.getDepCodeBranch(schoolCode, depType);
        return ResultEntity.success(depCodeSchool);
    }
}
